<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

/**
 * 我的客户
 *
 * @author Eric
 */
class MyCustomer
{

    private $myCustomer;

    public function __construct()
    {
        $this->myCustomer = M("my_customer");
    }

    /**
     * //获取我的客户基本信息
     *
     * @param $mc_id
     */
    public function getCustomer($map)
    {
        return $this->myCustomer->where($map)->find();
    }

    /**
     * //获取我的客户基本信息
     *
     * @param $mc_id
     */
    public function getCustomers($map)
    {
        return $this->myCustomer->where($map)->order("mc_id")->select();
    }

    /**
     * //新地我的客户
     *
     * @param $data
     *
     * @return mixed
     */
    public function customerAdd($data)
    {
        //新增车主
        $info = [];
        $info['phone'] = $data['phone'];
        $info['seller_name'] = $data['name'];
        $info['comefrom'] = '42';
        $info['modify_time'] = date('Y-m-d H:i:s');
        $info['posttime'] = date('Y-m-d H:i:s');
        $owner_id = \AuctionCore\CarOwner\Owner::create($info);
        //新增我的客户
        $data['posttime'] = date('Y-m-d H:i:s');
        $data['owner_id'] = $owner_id;
        $ret = $this->myCustomer->add($data);
        //
        M()->startTrans();
        if ($ret && $owner_id) {
            M()->commit();
        } else {
            M()->rollback();
        }
        //comefrom:APP端-经纪人
        return $ret;
    }


    /**
     *
     * @param type $page
     * @param type $size
     * @param type $map
     *
     * @return type
     */
    public static function getMyCustomerList($page, $page_size, $params)
    {
        $page = $page < 1 ? 1 : (int)$page;
        $page_size = $page_size < 1 ? 10 : (int)$page_size;
        $map = [];
        /**
         * 搜索条件：1、客户id 2、用户ID 3、客户姓名 4、客户手机号 5、经纪人ID 6、经纪人帐号 7、车辆编号 8、开始时间 9、结束时间
         */
        if ($params['mc_id']) {
            $map['mc.mc_id'] = ['LIKE', '%' . $params['mc_id'] . '%'];
        }
        if ($params['u_id']) {
            $map['co.uid'] = ['LIKE', '%' . $params['u_id'] . '%'];
        }
        if ($params['name']) {
            $map['mc.name'] = ['like', '%' . $params['name'] . '%'];
        }
        if ($params['phone']) {
            $map['mc.phone'] = ['like', '%' . $params['phone'] . '%'];
        }
        if ($params['agent_id']) {
            $map['mc.uid'] = ['eq', $params['agent_id']];
        }
        if ($params['agent_phone']) {
            $agent_phone = trim($params['agent_phone']);
            $user = new \AuctionCore\Account\FrontUser();
            $info = $user->get_user_info_by_pwd_mobile($agent_phone);
            $map['mc.uid'] = ['eq', $info['uid']];
        }
        if ($params['car_no']) {
            $map['c.sno'] = ['like', '%' . $params['car_no'] . '%'];
        }
        if ($params['start_time']) {
            $map['mc.posttime'] = ['egt', $params['start_time'] . ' 00:00:00'];
        }
        if ($params['end_time']) {
            $map['mc.posttime'] = ['elt', $params['end_time'] . ' 23:59:59'];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['mc.posttime'] = [['egt', $params['start_time'] . ' 00:00:00'], ['elt', $params['end_time'] . ' 23:59:59']];
        }
        $cols = 'mc.mc_id,mc.name,mc.phone,mc.posttime,co.uid,
                c.mbrand_id,c.brand_id,c.series_id,c.model_id,c.car_id,c.car_no,c.sno,c.car_cc,c.turbo,c.gearbox';
        $order = 'mc.mc_id DESC';
        $prefix = C('DB_PREFIX');
        $total = M("my_customer")->alias('mc')
            ->join('left join ' . $prefix . "agent AS ag ON mc.uid=ag.uid")
            ->join('left join ' . $prefix . "car_owner  AS co ON mc.owner_id = co.owner_id")
            ->join("left join " . $prefix . 'cars AS c ON co.owner_id = c.owner_id')
            ->where($map)->count();
        $list = M("my_customer")->alias('mc')->field($cols)
            ->join('left join ' . $prefix . "agent AS ag ON mc.uid=ag.uid")
            ->join('left join ' . $prefix . "car_owner  AS co ON mc.owner_id = co.owner_id")
            ->join("left join " . $prefix . 'cars AS c ON co.owner_id = c.owner_id')
            ->where($map)
            ->order($order)
            ->page($page)
            ->limit($page_size)
            ->select();
        $cm = new \AuctionCore\CarResource\CarModel();
        foreach ($list as $k => $v) {
            $broker = \AuctionCore\Broker\Broker::get_detail_by_uid($v['uid']);
            $user = new \AuctionCore\Account\FrontUser();
            $info = $user->get_user_info_by_uid($broker['uid']);
            $list[$k]['agent_user'] = $info['pwd_mobile'];
            if (!empty($v['car_id'])) {
                $series = $cm->get_model_by_id($v['series_id'], 'series');
                $list[$k]['car_model_show'] = $series['name'] . ' ' . $v['car_cc'] . ($v['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $v['gearbox']);;
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total,
        ];
        return $result;
    }


    /**
     * //获取我的客户列表
     *
     * @param $page
     * @param $size
     * @param $params
     */
    public function myCustomers($page, $size, $map)
    {
        $total = $this->myCustomer->where($map)->count();
        $ret = $this->myCustomer->where($map)->limit($size)->page($page)->order('mc_id DESC')->select();
        $list = [];
        foreach ($ret as $key => $val) {
            $list[$key]['cid'] = $val['mc_id'];
            $list[$key]['phone'] = $val['phone'];

            //我的客户头像
            if (\AuctionCore\Account\FrontUser::is_user_exists($val['phone'])) {
                $u = new \AuctionCore\Account\FrontUser($val['phone']);
                $info = $u->user_info();
                $list[$key]['image'] = $info['avatar'];
            } else {
                $list[$key]['image'] = '';
            }

            //我的客户备注
            $rs = \AuctionCore\Broker\Broker::get_remark_name($val['uid'], $val['mc_id'], 3);
            $list[$key]['remark'] = $rs['remark'];
            //我的客户车源状态
            $list[$key]['state'] = \AuctionCore\CarOwner\Owner::get_car_status($val['owner_id']);
        }
        return [$list, $total];
    }

    /**
     * 获取我的客户详情
     *
     * @param $uid
     * @param $mc_id
     */
    public function myCustomerDetail($uid, $mc_id)
    {
        $ret = $this->getCustomer(['mc_id' => ['eq', $mc_id]]);
        $detail = [];
        $detail['cid'] = $ret['mc_id'];
        $detail['owner_id'] = $ret['owner_id'];
        $detail['phone'] = $ret['phone'];
        //我的客户头像
        if (\AuctionCore\Account\FrontUser::is_user_exists($ret['phone'])) {
            $u = new \AuctionCore\Account\FrontUser($ret['phone']);
            $info = $u->user_info();
            $detail['image'] = $info['avatar'];
        } else {
            $detail['image'] = '';
        }
        //我的客户备注
        $ret = \AuctionCore\Broker\Broker::get_remark_name($uid, $mc_id, 3);
        $detail['remark'] = $ret['remark'];
        //我的客户车源状态
        $detail['state'] = \AuctionCore\CarOwner\Owner::get_car_status($detail['owner_id']);
        return $detail;
    }

    public function updateById($id, $data)
    {
        if ($id < 0 || empty($data)) {
            return false;
        }
        return $this->myCustomer->where(['mc_id' => $id])->save($data);
    }

}
